
# Create your views here.
from django.shortcuts import render
from models import Category
from django.http import HttpResponse
'''
from models import  Item,ItemForm
from settings import MEDIA_ROOT 
from models import PickItemForm
from settings import MEDIA_ROOT 
def index(request,number=4):
    pickitemform=PickItemForm()
    item=Item.objects.get(iditem=number)
    form=ItemForm(instance=item)
    categ=categories()
    x=Item.objects.get(iditem=number).photo.name
    return HttpResponse(render(request,'store/index.html', {'items':categ,'id':number,'form':form, 'x':x,'pickitemform':pickitemform}))

def item(request,id):
    if request.method == 'POST': # If the form has been submitted...
        form = ItemForm(request.POST,request.FILES,instance=Item.objects.get(iditem=id)) # A form bound to the POST data
        if form.is_valid():
            form.save(commit=True)
    x=Item.objects.get(iditem=id).photo.name
    return HttpResponse(render(request,'store/index.html',
                                {'items':categories, 'id':id,'form':form,'x':x}))
    
def getItem(request):
    if request.method=='POST':
        form=PickItemForm(request.POST)
        if form.is_valid():
            return index(request,form.cleaned_data['iditem'])
def categories():
    categories=[]
    items = Item.objects.all()
    for s in items:
        #rendereditems.append(s.__unicode__()[s.__unicode__().rfind('>')+1:])
        categories.append(s.getType())
    return set(categories)
'''

def index(request):
    categories = Category.objects.all()
    catnames = []
    for x in categories[:]:
        catnames.append(x.name.encode('utf-8'))
    catnames.sort(key=str.lower, reverse=False)
    return HttpResponse(render(request,'store/index2.html',{'catnames':catnames}))